<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        try
        {        
        Exception ex = Server.GetLastError().GetBaseException();
        string strEx = "MESSAGE: " + ex.Message + "\nSOURCE: " + ex.Source + "\nFORM: " + Request.Form.ToString() + "\nQUERYSTRING: " + Request.QueryString.ToString() + "\nTARGETSITE: " + ex.TargetSite + "\nSTACKTRACE: " + ex.StackTrace;
        SiteManager.Manager man = new SiteManager.Manager();
        if (System.Configuration.ConfigurationManager.AppSettings["SendErrorMail"].Equals("Y"))
            man.sendMail(strEx,"testingsynapse@gmail.com","Findadoc.com","FindaDoc.com: Exception thrown","info@findadoc.com","");       
        }
        catch
        {
        }
        if (System.Configuration.ConfigurationManager.AppSettings["ShowFriendlyError"].Equals("Y"))
            Response.Redirect("~/Error/FriendlyError.aspx", true);

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started          

    
        // Session["StrHttp"] = Request.ApplicationPath.ToString() + "/"; //System.Configuration.ConfigurationManager.AppSettings["strHttp"].ToString();
        Session["StrHttp"] = System.Configuration.ConfigurationManager.AppSettings["strHttp"].ToString();
        Session["AdminPageSize"] = System.Configuration.ConfigurationManager.AppSettings["AdminPageSize"].ToString();
        //Session["StrHttps"] = System.Configuration.ConfigurationSettings.AppSettings["strHttps"].ToString(); 
        Session["StrHttps"] = System.Configuration.ConfigurationManager.AppSettings["strHttps"].ToString(); 

        //All Variables for Admin Acess
        Session["ADMIN_ID"] = "";
        Session["ACCESS_LEVEL"] = "";
        Session["UserName"] = "";
        Session["Password"] = "";
        Session["User"] = "";
        Session["FeaDoctorsId"] = "";
        Session["Top3"] = "";
        Session["sDoctorsId"] = "";
        //end
        //for main site
        Session["sDoctorsId"] = "";
        Session["pageFromOrAction"] = "";
        Session["userID"] = "";
        Session["userType"] = "";
        Session["LoginName"] = "";
        Session["UserEmail"] = "";
        Session["DoctorType"] = "";
        Session["DentistDoctorEmail"] = "";
        
        // for header links
        Session["Reg"] = "";
        Session["Clicked"] = "";
        Session["NoDocResult"] = "";
        Session["NoDenResult"] = "";
        Session["Travel_Dis"] = "";
        Session["Speciality"] = "";

        Session["ShowLayer"] = "";

        Session["PreviousSpeciality"] = "";
        Session["PreviousSubSpeciality"] = "";

        
        localFunction lfunc = new localFunction();
        try
        {
            string password;
            string username;
            bool check;

            //  Read Doctor Cookie
            lfunc.ReadCookie(localFunction.DOCTORCOOKIE, out  username, out  password, out check);
            // Check For Values
            if (username != "" && password != "")
                (new Doctor()).DoLogin(username, password, "NOREDIRECT");

            //  Read Dentist Cookie
            lfunc.ReadCookie(localFunction.DENTISTCOOKIE, out  username, out  password, out check);
            // Check For Values
            if (username != "" && password != "")
                (new clsDentist()).DoDentistLogin(username, password, "NOREDIRECT");

            //  Read Patient Cookie
            lfunc.ReadCookie(localFunction.PATIENTCOOKIE, out  username, out  password, out check);
            // Check For Values
            if (username != "" && password != "")
                (new Patient()).DoPatientLogin(username, password, "NOREDIRECT");
            
            lfunc = null;
        }
        catch { }
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
       
        //Response.Write(Request.Url.ToString() + " " + Session["strHttp"].ToString());
        //if (Request.Url.ToString() == "http://findadoc.com" || Request.Url.ToString() == "http://www.findadoc.com/default.aspx" || Request.Url.ToString() == "http://findadoc.com/default.aspx")


        if (System.Configuration.ConfigurationManager.AppSettings["Use301Redirect"].Equals("Y"))
        {
            if (!Request.Url.ToString().ToLower().Contains("http://www.") && !Request.Url.ToString().ToLower().Contains("https://www."))
            {
                string url = Request.Url.ToString().ToLower();
                url = url.Replace("http://", "http://www.");
                

                if (url.ToLower().Equals("http://www.findadoc.com/default.aspx") || url.ToLower().Equals("http://www.findadoc.com/"))
                {
                    Response.Status = "301 Moved Permanently";
                    url = "http://www.findadoc.com";
                }
                Response.AddHeader("Location", url);
            }
        }
    }
</script>
